/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-02 21:47:42
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-10 17:00:22
 * @FilePath: \keep_motion_api\controllers\user\member.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Member } = require('@/models/user');
const getMembers = async (req, res) => {
  try {
    const { level, expire_date, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Member.getMembers(level, expire_date, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询会员失败', error });
  }
}
const createMember = async (req, res) => {
  try {
    const { user_id, level, expire_date, benefits, course_discounts } = req.body;
    const member = await Member.create({ user_id, level, expire_date, benefits, course_discounts });
    res.status(201).send(member);
  } catch (error) {
    res.status(500).json({ message: '创建会员失败', error });
  }
}
const updateMember = async (req, res) => {
  try {
    const { id } = req.params;
    const { user_id, level, expire_date, benefits, course_discounts } = req.body;
    const member = await Member.updateMember(id, { user_id, level, expire_date, benefits, course_discounts });
    res.status(200).send(member);
  } catch (error) {
    res.status(500).json({ message: '更新会员失败', error });
  }
}
const deleteMember = async (req, res) => {
  try {
    const { id } = req.params;
    const member = await Member.deleteMember(id);
    res.status(200).send(member);
  } catch (error) {
    res.status(500).json({ message: '删除会员失败', error });
  }
}

module.exports = {
  getMembers,
  createMember,
  updateMember,
  deleteMember 
}
